package com.hntxk.cqzd.diacrisis.service.impl;/**
 * @ClassName: ${user}
 * @Author:liuqing
 * @Description
 * @Date: Created in 10:12 2020/5/21
 * @Modified By:
 */

import com.hntxk.core.framework.service.impl.BaseServiceImpl;
import com.hntxk.core.util.NumberUtil;
import com.hntxk.core.util.StringUtil;
import com.hntxk.cqzd.diacrisis.entity.SelBySisIdEntity;
import com.hntxk.cqzd.diacrisis.entity.TblDiacrisisInfo;
import com.hntxk.cqzd.diacrisis.service.DiagnosticProcessService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * @ClassName DiagnosticProcessServiceImpl
 * @description 诊断信息流程展示service实现
 * @Author Liuqing
 * @Date 2020/5/21 10:12
 * @Version 1.0
 */
@Service("processService")
public class DiagnosticProcessServiceImpl extends BaseServiceImpl<TblDiacrisisInfo> implements DiagnosticProcessService {

    private static String MAPPER_NAMESPACE="com.hntxk.cqzd.diacrisis.mapper.DiagnosticProcessMapper.";

    public DiagnosticProcessServiceImpl() {
        super(MAPPER_NAMESPACE);
    }

    @Override
    public List<TblDiacrisisInfo> listQueryWhere(TblDiacrisisInfo queryWhere) {
        List<TblDiacrisisInfo> entity = getDasTemplate().selectList(MAPPER_NAMESPACE + "list", queryWhere);

        for (TblDiacrisisInfo infoModel: entity) {
            if ( StringUtil.isNotNull(infoModel.getFruitTypeName()) && StringUtil.isNotNull(infoModel.getFruitExplain())) {
                if(infoModel.getFruitTypeName().equals("核型分析")){
                    if (StringUtil.isNotNull(infoModel.getFruitExplain())){
                        infoModel.setDiagnosisStatus(5);
                    }
                }else
                if (infoModel.getFruitTypeName().equals("FISH")) {
                    if (StringUtil.isNotNull(infoModel.getFruitExplain())) {
                        infoModel.setDiagnosisStatus(4);
                    }

                } else if (infoModel.getFruitTypeName().equals("CNV")) {
                    if (StringUtil.isNotNull(infoModel.getFruitExplain())) {
                        infoModel.setDiagnosisStatus(3);
                    }
                } else if (infoModel.getInspectionTime() != null) {
                    infoModel.setDiagnosisStatus(2);
                }
            }else {
                infoModel.setDiagnosisStatus(0);
            }

        }

        List<TblDiacrisisInfo> nullStatus = new ArrayList<>();
        List<TblDiacrisisInfo> status = new ArrayList<>();

        for (TblDiacrisisInfo infoModel : entity) {
            if (NumberUtil.isNull(infoModel.getDiagnosisStatus())){
                nullStatus.add(infoModel);
            }else {
                status.add(infoModel);
            }

        }

        Collections.sort(status, Comparator.comparing(TblDiacrisisInfo::getDiagnosisStatus).reversed());
        if (!CollectionUtils.isEmpty(nullStatus)) {
            status.addAll(nullStatus);
        }

         entity = status;

        List<TblDiacrisisInfo> result = new ArrayList<>();

        Map<Integer,TblDiacrisisInfo> seen = new ConcurrentHashMap<>();

        List<TblDiacrisisInfo> collect = new ArrayList<>();


        for(TblDiacrisisInfo info : entity){
            if(info.getDiagnosisId() == null){
                collect.add(info);
                continue;
            }
            //putIfAbsent 将值插入到map中，如果对应key存在value值则返回存在的value值，不进行替换，如果不存在添加值，返回null
            if(seen.putIfAbsent((info.getDiacrisisId().intValue()),info) == null){
                collect.add(info);

            }
        }

//        List<TblDiacrisisInfo> nullRecordTime = new ArrayList<>();
//        List<TblDiacrisisInfo> recordTime = new ArrayList<>();

//        for (TblDiacrisisInfo diacrisisInfo : collect
//        ) {
//            if (diacrisisInfo.getRecordTime() == null) {
//                nullRecordTime.add(diacrisisInfo);
//            }
//            if (diacrisisInfo.getRecordTime() != null){
//                recordTime.add(diacrisisInfo);
//            }
//        }
//        if (recordTime.size()>2) {
//            Collections.sort(recordTime, Comparator.comparing(TblDiacrisisInfo::getRecordTime).reversed());
//        }
//        collect.clear();
//        collect.addAll(recordTime);
//        collect.addAll(nullRecordTime);

        //根据初诊日期降序排序
        Collections.sort(collect, Comparator.comparing(TblDiacrisisInfo::getRecordTime).reversed());

        if (!CollectionUtils.isEmpty(collect)) {
            queryWhere.getPagination().setTotal(collect.size());

            if (0 == queryWhere.getPagination().getPageIndex()) {
                result = collect.stream().skip(0).limit(queryWhere.getPagination().getPageSize()).collect(Collectors.toList());
            } else {
                //计算当前页跳过的条数
                int skip = (queryWhere.getPagination().getPageIndex() - 1) * queryWhere.getPagination().getPageSize();
                result = collect.stream().skip(skip).limit(queryWhere.getPagination().getPageSize()).collect(Collectors.toList());
            }
        }else {
            queryWhere.getPagination().setTotal(0);
        }


      return result;

    }

    @Override
    public List<SelBySisIdEntity> selBySisId(Long diacrisisId) {
        return getDasTemplate().selectList(MAPPER_NAMESPACE + "selBySisId", diacrisisId);
    }


//    private <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
//        Map<Object,Boolean> seen = new ConcurrentHashMap<>();
//        try {
//            return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
//        } catch (NullPointerException e) {
//            //如果入参为空返回true  保留该条数据
//            return t -> true;
//        }
//    }

}
